﻿#region | Copyright © 2556 |

/*
##########################################################################
	Author By : Siriwat Suananpornpanit
	DateTime : 2556/08/30
##########################################################################
*/

#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using DevExpress.Web.ASPxPopupControl;

namespace Web.Core
{
    public sealed class Modal
    {
        #region | Partial Modal Paging |

        public static void OnControl(
            System.Web.UI.Page page
            ,ASPxPopupControl control
            , ModalFlag modalBox)
        {
            try
            {
                if (modalBox != ModalFlag.None)
                {
                    if (modalBox == ModalFlag.Show)
                    {
                        control.Focus() ;
                        // Action Closed
                        control.ShowOnPageLoad = false;
                        // Action Opening
                        control.ShowOnPageLoad = true;
                    }
                    else
                    {
                        control.ShowOnPageLoad = false;
                    }
                }
            }
            catch (System.Exception ex)
            {
                Exception(
                    page: page,
                    message: ex.Message);
            }
        }

        public static void OnToggleModal(
            ASPxPopupControl control
            ,System.Web.UI.Page page
            , ModalFlag modalBox
            , MessageFlag messageBox
            , String massage
            , Int32 pageiIdx = 0
            , String headerText = null
            )
        {
            // Validation Control
            if (control != null)
                Core.Modal.OnControl(
                                    page: page,
                                    control: control,
                                    modalBox: modalBox);
            // Reload
            if (messageBox == MessageFlag.Completed)
                OnReload(
                        page: page,
                        pageiIdx: pageiIdx,
                        headerText: headerText);

            // Show Alert
            if (!String.IsNullOrEmpty((String)massage))
                MasterToggle(
                            page: page,
                            messageBox: messageBox,
                            massage: massage);

        }

        public static void Toggle(
            System.Web.UI.Page page
            , ModalFlag modal
            , MessageFlag flag
            , String massage
            , String headerText)
        {
            try
            {Assembly.GetInstance().OnReflectMethod(
               page: page,
               methods: ModalState.GetInstance().State(
                                                    ModalState.OnToggleModal), 
               message: new object[]
               {
                   (ModalFlag) modal
                   , 
                   (MessageFlag)flag
                   , 
                   (String)massage
                   ,
                   (String) headerText
               });
            }
            catch (System.Exception ex)
            {
                Exception(
                    page: page,
                    message: ex.Message,
                    headerText: null);
            }
        }

        private static void OnReload(
            System.Web.UI.Page page
            , Int32 pageiIdx 
            , String headerText)
        {
            try
            {
                Core.Assembly.GetInstance().OnReflectMethod(
                    page: page,
                    methods: ModalState.GetInstance().State(
                                                        ModalState.OnReload), 
                    message: new object[] { (Int32)pageiIdx });
            }
            catch (System.Exception ex)
            {
                Exception(
                    page: page,
                    message: ex.Message,
                    headerText: headerText);
            }
        }

        #endregion

        #region | Modal of MastePage |

        public static void Exception(
            System.Web.UI.Page page
            , String message
            , String headerText = null)
        {
            try
            {Assembly.GetInstance()
                    .OnReflectMasterPage(
                    master: page.Master,
                    methods: ModalState.GetInstance().State(
                                                        ModalState.OnException), 
                    message: new object[]
                    {
                        (String)message
                    });
            }
            catch (System.Exception ex)
            {
                new SystemException(ex.Message);
            }

            if (!String.IsNullOrEmpty(headerText))
                    HeaderText(
                        page: page, 
                        headerText: (String) headerText);

        }

        private static void MasterToggle(
            System.Web.UI.Page page
            ,MessageFlag messageBox
            , String massage
            , String headerText = null)
        {
            try
            {Assembly.GetInstance().OnReflectMasterPage(
                master: page.Master,
                methods: ModalState.GetInstance().State(
                                                    ModalState.OnToggleModal), 
                message: new object[]
                {
                    (MessageFlag)messageBox
                    , 
                    (String)massage
                });
            }
            catch (System.Exception ex)
            {
                Exception(
                    page: page,
                    message: ex.Message,
                    headerText: null);
            }

            if (!String.IsNullOrEmpty(headerText))
                HeaderText(
                    page: page, 
                    headerText: (String)headerText);

        }

        private static void HeaderText(
            System.Web.UI.Page page
            ,String headerText = null)
        {
            if (!String.IsNullOrEmpty(headerText))
            {
                try
                {
                    Assembly.GetInstance().OnReflectMethod(
                        page: page,
                        methods: ModalState.GetInstance().State(
                                                            ModalState.OnToggleHeaderText),
                        message: new object[]
                        {
                            (String)headerText
                        });
                }
                catch (System.Exception ex)
                {
                    Exception(
                        page: page,
                        message: ex.Message,
                        headerText: null);
                }
            }
        }

        #endregion
    }
}